<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="f"%>

	<form id="form">
	  <div class="form-group">
	    <label for="titleid">文章标题</label>
	    <input type="text" name="title" class="form-control" id="titleid" aria-describedby="titleHelp">
	    <small id="titleHelp" class="form-text text-muted">请输入文章标题 最少2个汉字 最多不超过200个.</small>
	  </div>
	  
	   <div class="form-group">
	    <label for="channelId">所属栏目</label>
	    <select class="form-control" name="channel_id" id="channelId" onchange="channelChange()">
	     	<c:forEach items="${channels}" var="channel" >
	     		<option value="${channel.id}">${channel.name}</option>
	     	</c:forEach>
	    </select>
	  </div>
	  
	   <div class="form-group">
	    <label for="catId">所属分类</label>
	    <select class="form-control"  name="category_id" id="catId" >
	      <option>1</option>
	    </select>
	  </div>
	  
	   <div class="form-group">
	    <label for="exampleFormControlFile1" >标题图片</label>
	    <input type="file" name="imgFile" class="form-control-file" id="exampleFormControlFile1">
	  </div>
	  
	  <div class="mb-3">
	    <label for="validationTextarea">文章内容</label>
	    <textarea class="form-control is-invalid" name="content" id="validationTextarea" placeholder="这里写入文章的内容" required></textarea>
	    <div class="invalid-feedback">
	      	请在这里输入文章内容
	    </div>
	  </div>
	  
	  <input type="button" class="btn btn-primary" value="添加" onclick="add()"/>
	</form>
	
	
	<script type="text/javascript">
	
		var editor1 = null;
	
		function channelChange(){
			var channelID = $("select[name='channel_id']").val();
			$("#catId").empty();
			$.post("/article/findCategory",{channelID:channelID},function(msg){
				
				console.log(msg.t);
				
				for(var i in msg.t){
					
					$("#catId").append("<option value='"+msg.t[i].id+"'>"+msg.t[i].name+"</option>");
					
				}
				
			},"json");		
			
		}
		
		
		function add(){
			
			var formdata=new FormData($("#form")[0]);
			
			formdata.append("content",editor1.html());
			
			$.ajax({
				
				type :'post',
		           url : '/article/addArticleFile',
		           data :formdata,
		           cache : false,
		           processData : false, // 不处理发送的数据，因为data值是Formdata对象，不需要对数据做处理
		           contentType : false, // 不设置Content-type请求头
		           success : function(msg){
		        	   if(msg.errorid==0){
		        		   alert('发表成功');
		        		   location = "/article/toHome";
		        	   }else{
		        		   var errmsg = msg.errorMsg;
		        		   alert(errmsg);
		        	   }
		           },
		           error : function(){ }
				
			})
				
		}
		
		
		$(function(){
			KindEditor.ready(function(K) {
				
				editor1 = K.create('#validationTextarea', {
					cssPath : '/js/kindeditor/plugins/code/prettify.css',
					uploadJson : '/file/uploads.do',
					fileManagerJson : 'dssd',
					allowFileManager : true,
					afterCreate : function() {
						var self = this;
						K.ctrl(document, 13, function() {
							self.sync();
							document.forms['form'].submit();
						});
						K.ctrl(self.edit.doc, 13, function() {
							self.sync();
							document.forms['form'].submit();
						});
					}
				});
				//prettyPrint();
			});
			})
		
	
	</script>	
	
	

